package com.base.system.managedBean;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.html.impl.UIIFrame;
import org.operamasks.faces.component.layout.impl.UIWindow;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.security.SecurityConst;
import com.base.security.dao.IResourceDao;
import com.base.security.entity.Resource;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "modules.system.webadmin.resourceListBean", scope = ManagedBeanScope.REQUEST)
public class ResourceListBean implements Serializable {

	private final static Log logger = LogFactory.getLog(ResourceListBean.class);
	protected IResourceDao resourceDao = SpringBeanHolder
			.getBean("resourceDao");
	@Inject
	private FacesContext context;
	@SaveState
	@Bind(id = "keywords", attribute = "value")
	private String keywords;
	@SaveState
	@Bind(id = "valueSearch", attribute = "value")
	private String valueSearch;
	@SaveState
	@Bind(id = "displayNameSearch", attribute = "value")
	private String displayNameSearch;
	@Bind(id = "code", attribute = "value")
	private String code;
	@SaveState
	@Bind(id = "modulesSearch", attribute = "value")
	private String modulesSearch;
	@Bind(id = "resourceGrid", attribute = "binding")
	private UIDataGrid resourceGrid;

	@SuppressWarnings("unchecked")
	@DataModel(id = "resourceGrid")
	private List<Resource> getResources() {
		String hql = "select sr from Resource as sr where 1=1";
		if (displayNameSearch != null && !"".equals(displayNameSearch)) {
			hql = hql + " and sr.displayName like '%" + displayNameSearch
					+ "%'";
		}
		if (valueSearch != null && !"".equals(valueSearch)) {
			hql = hql + " and sr.value like '%" + valueSearch + "%'";

		}
		if (modulesSearch != null && !"".equals(modulesSearch)) {
			hql = hql + " and sr.module like '%" + modulesSearch + "%'";
		}
		hql = hql + " order by sr.module";

		// if (displayNameSearch != null && !"".equals(displayNameSearch)
		// && valueSearch != null && !"".equals(valueSearch)) {
		// return resourceDao.findAllByNamedQuery(
		// "resource.searchByValueAndDisplayName", "%" + valueSearch
		// + "%", "%" + displayNameSearch + "%");
		// } else if (displayNameSearch != null &&
		// !"".equals(displayNameSearch)) {
		// return resourceDao.findAllByNamedQuery("resource.getByDisplayName",
		// "%" + displayNameSearch + "%");
		// } else if (valueSearch != null && !"".equals(valueSearch)) {
		// return resourceDao.findAllByNamedQuery("resource.searchByValue",
		// "%" + valueSearch + "%");
		// }
		return resourceDao.findByHql(hql);
	}

	@Bind(id = "detailDialog")
	protected UIWindow detailDialog;
	@Bind(id = "frame")
	private UIIFrame frame;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		logger.debug("Listbean 中的 postBack:" + isPostback);
		if (!isPostback) {
			valueSearch = null;
			displayNameSearch = null;
			modulesSearch = null;
		}
	}

	private boolean initSelected() {
		Object[] objs = resourceGrid.getSelectedValues();
		// System.out.println("取得的选取值是：" + objs);
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return false;
		}
		context.getExternalContext().getSessionMap().put(
				SecurityConst.RESOURCE_SELECT_ROW_DATA, (Resource) objs[0]);
		return true;
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	@Action(id = "gridReload")
	public void gridReload() {
		detailDialog.close();
	}

	@Action(id = "createResource")
	public void createResource() {
		String url = "/modules/system/addResource.faces";
		url = context.getExternalContext().getRequestContextPath() + url;
		String script = "addCenterLayoutTab('" + SecurityConst.RESOURCE_ADD_TAB
				+ "','" + "新增资源" + "', '" + url + "')";
		Browser.execClientScript("window.top", script);
	}

	@Action(id = "editResource")
	public void editResource() {
		Object[] objs = resourceGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return;
		}
		Resource resource = (Resource) objs[0];
		detailDialog.setTitle("修改资源");
		frame.load("/modules/system/editResource.faces?resourceId="
				+ resource.getId());
		detailDialog.show();
	}

	@Action(id = "query")
	public void query() {
		resourceGrid.reload();
	}

	@Action(id = "reset")
	public void reset() {
		valueSearch = null;
		displayNameSearch = null;
		modulesSearch = null;
		resourceGrid.reload();
	}
}
